package com.geekq.miaosha.redis.redismanager;

import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Transaction;

import java.util.List;
import java.util.UUID;

public class RedisLock {

    public String getLock(String key , int timeOut){

        try {
                Jedis jedis =  RedisManager.getJedis();
                String value = UUID.randomUUID().toString();
                long end   =System.currentTimeMillis()+timeOut;
                while (System.currentTimeMillis()<end){
                    if(jedis.setnx(key,value) ==1){
                        jedis.expire(key, timeOut);
                        //锁设置成功 redis操作成功
                        return value;
                    }
                    if(jedis.ttl(key)== -1){
                        jedis.expire(key, timeOut);
                    }

                    Thread.sleep(1000);
                }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

/**
 * watch 监控多个key 一防止其他地方调用释放锁的时候对这个key进行修改 那么事务里面的代码就不会被执行 ！
 */
    public boolean releaseLock(String key , String value){
        try {
            Jedis jedis = RedisManager.getJedis();
            while (true){
                jedis.watch(key);
                if(value.equals(jedis.get(key))){
                    Transaction transaction = jedis.multi();
                    transaction.del(key);
                    List<Object> list = transaction.exec();
                    if(list ==null){
                        continue;
                    }
                    jedis.unwatch();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
